// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/MainLayout.vue'
import Home from '../views/HomeView.vue'
import designer from '../designer/Index.vue'
import login from '../views/Login.vue'
import pages from '../views/Pages.vue'
import components from '../views/Components.vue'
import commodityList from '../views/commodity/CommodityList.vue'
import category from '../views/Category.vue'
import news from '../views/news/News.vue'

const routes = [
    { path: '/login', component: login },
    {
        path: '/',
        component: Layout,
        children: [
            {
                path: '',
                component: Home,
                meta: { title: '首页', icon: 'HomeIcon' }
            },
            {
                path: 'pages',
                component: pages,
                meta: { title: '页面', icon: 'HomeIcon' }
            },
            {
                path: 'components',
                component: components,
                meta: { title: '组件管理', icon: 'HomeIcon' }
            },
            {
                path: 'commodityList',
                component: commodityList,
                meta: { title: '商品管理', icon: 'HomeIcon' }
            },
            {
                path: 'categoryList',
                component: category,
                meta: { title: '分类管理', icon: 'HomeIcon' }
            },
            {
                path: 'news',
                component: news,
                meta: { title: '新闻管理', icon: 'HomeIcon' }
            },
        ]
    },
    {
        path: '/designer',
        component: designer
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 登录拦截
router.beforeEach((to, from, next) => {
    if (to.path !== '/login') {
        const loggedIn = localStorage.getItem('isLoggedIn')
        if (!loggedIn) return next('/login')
    }
    next()
})

export default router
